﻿//======================================================================
//
//        Copyright (C) 2002-2008 S.Sams Lifexperience!
//        All rights reserved
//        FileName : TempHelp
//        Description :
//        Created by Sam Shen at 2008/8/2 18:21:40
//        http://lab.travelive.com.cn/
//        E-Mail: S.Sams@msn.com
//
//======================================================================

using System;
using System.Collections.Generic;
using System.Text;

namespace S.Sams.GoogleMap
{
    /// <summary>
    /// 模板操作类
    /// </summary>
    internal class TempHelp
    {
        /// <summary>
        /// 显示Js调用名称
        /// </summary>
        /// <param name="lab"></param>
        /// <returns></returns>
        public static string ShowMsgName(List<Label> lab)
        {
            string showName = string.Empty;
            for(int i=0; i<lab.Count; i++)
            {
                showName += ",mapshowmsg" + i.ToString();
            }
            return showName;
        }

        /*
         var setmapmsg1 = document.createElement("div");
		setmapmsg1.innerHTML = "<div><strong>中国广东省广州市东山区二沙岛</strong></div><img src='http://jp.yiqifei.com/images/logo.png' />";
         
		mapshowmsg1 = function(){
			map.setCenter(new GLatLng(23.110300,113.296400), big);
			map.openInfoWindow(map.getCenter(),setmapmsg1);
		};
        letteredIcon.image = "http://www.google.com/mapfiles/markerA.png";

        
        
		var marker = new GMarker(new GLatLng(23.110300,113.296400),letteredIcon);
		GEvent.addListener(marker, 'click', function(){
			mapshowmsg1();
		});
		map.addOverlay(marker);
         */
        /// <summary>
        /// 生成标识
        /// </summary>
        /// <param name="labs"></param>
        /// <param name="isshowcentermsg"></param>
        /// <param name="InitZoomIn"></param>
        /// <returns></returns>
        public static string BuildeLabes(List<Label> labs, bool isshowcentermsg, int InitZoomIn)
        {
            StringBuilder sb = new StringBuilder();
            int i = 0;
            bool isSetCenter = false;
            foreach (Label lab in labs)
            {
                if (lab.Visible)
                {
                    string SetImgStr = string.Empty;
                    sb.Append("var setmapmsg" + i + " = document.createElement(\"div\");");

                    if (lab.TabMsg != null)
                    {
                        SetImgStr = CreateTabMsg(lab.TabMsg, i);
                    }
                    else if (!string.IsNullOrEmpty(lab.Msg))
                    {
                        sb.Append(
                            string.Format("setmapmsg{0}.innerHTML = \"{1}\";",
                            i.ToString(),
                            lab.Msg.Replace("\"", "'"))
                        );
                        SetImgStr = "map.openInfoWindow(marker" + i.ToString() + ".getPoint(),setmapmsg" + i.ToString() + ShowMaxContent(lab) + ");";
                    }
                    else if (string.IsNullOrEmpty(lab.Msg))
                    {
                        SetImgStr = "map.panTo(marker" + i.ToString() + ".getPoint());";
                    }

                    if (lab.IsShowMapView)
                    {
                        SetImgStr = "map.showMapBlowup(marker" + i.ToString() + ".getLatLng());";
                    }

                    //sb.Append("mapshowmsg" + i.ToString() + " = function(){map.closeInfoWindow(); map.panTo(new GLatLng(" + lab.X.ToString() + "," + lab.Y.ToString() + "));" + SetBigPoint(lab.ZoomIn, InitZoomIn) + "" + SetImgStr + "};");
                    //sb.Append("mapshowmsg" + i.ToString() + " = function(){map.closeInfoWindow(); map.panTo(marker" + i.ToString() + ".getPoint());" + SetBigPoint(lab.ZoomIn, InitZoomIn) + SetImgStr + "};");
                    sb.Append("mapshowmsg" + i.ToString() + " = function(){map.closeInfoWindow(); " + SetBigPoint(lab.ZoomIn, InitZoomIn) + SetImgStr + "};");

                    if (string.IsNullOrEmpty(lab.LabelImage))
                    {
                        sb.Append("letteredIcon.image = \"" + Helper.LabelDefaultImage(i) + "\";");
                    }
                    else
                    {
                        sb.Append("letteredIcon.image = \"" + lab.LabelImage + "\";");
                    }

                    sb.Append("var marker" + i.ToString() + " = new GMarker(new GLatLng(" + lab.X.ToString() + "," + lab.Y.ToString() + "),letteredIcon);GEvent.addListener(marker" + i.ToString() + ", 'click', function(){mapshowmsg" + i.ToString() + "()});map.addOverlay(marker" + i.ToString() + ");");

                    if (lab.IsCenter && (!isSetCenter))
                    {
                        sb.Append("map.setCenter(new GLatLng(" + lab.X.ToString() + "," + lab.Y.ToString() + "));");
                        if (isshowcentermsg)
                        {
                            sb.Append("mapshowmsg" + i + "();");
                        }
                        isSetCenter = true;
                    }
                }

                i++;
            }
            return sb.ToString();
        }

        #region 设置消息放大数据
        /// <summary>
        /// 设置消息放大数据
        /// </summary>
        /// <param name="lab"></param>
        /// <returns></returns>
        private static string ShowMaxContent(Label lab)
        {
            //,{maxContent: "Html内容",maxTitle: "标题"}
            if (!string.IsNullOrEmpty(lab.MsgMaxContent))
            {
                return ",{maxContent: \"" + lab.MsgMaxContent.Replace("\"", "'") + "\",maxTitle: \"" + lab.MsgMaxTitle + "\"}";
            }
            return "";
        }
        #endregion

        #region 创建 Tab 消息列表
        /// <summary>
        /// 创建 Tab 消息列表
        /// </summary>
        /// <param name="Msgs"></param>
        /// <param name="id">当前ID</param>
        /// <returns></returns>
        private static string CreateTabMsg(List<Msg> Msgs, int id)
        {
            if (Msgs.Count == 0) return "";

            /*
             marker.openInfoWindowTabsHtml([
				new GInfoWindowTab("环境", "天坛公园很漂亮，祈年殿尤其不错"),
				new GInfoWindowTab("饮食", "仅次于一般"),
				new GInfoWindowTab("一起飞", "<div><strong>一起飞国际机票网</strong></div><img src='http://jp.yiqifei.com/images/logo.png' />")
			]);
             */

            string TabMsgs = "marker" + id.ToString() + ".openInfoWindowTabsHtml([";
            int i=0;
            foreach (Msg item in Msgs)
            {
                if (i.Equals(0))
                    TabMsgs += "new GInfoWindowTab(\"" + item.TabName + "\", \"" + item.TabContent.Replace("\"", "'") + "\")";
                else
                {
                    TabMsgs += ",new GInfoWindowTab(\"" + item.TabName + "\", \"" + item.TabContent.Replace("\"", "'") + "\")";
                }

                i++;
            }
            TabMsgs += "]);";
            return TabMsgs;
        }
        #endregion

        #region 设置ZoomIn值
        /// <summary>
        /// 如果放大系统大于0,就设置, 则用当前系统放大系统
        /// </summary>
        /// <param name="ZoomIn"></param>
        /// <param name="InitZoomIn"></param>
        /// <returns></returns>
        private static string SetBigPoint(int ZoomIn, int InitZoomIn)
        {
            if (ZoomIn > 0 && !ZoomIn.Equals(InitZoomIn))
            {
                //return "," + ZoomIn.ToString();

                //map.setCenter(new GLatLng(39.917, 116.397), 14);
                return "map.setCenter(map.getCenter(), " + ZoomIn.ToString() + ");";
            }
            return "";
        }
        #endregion
    }
}
